package com.lazybones.base.engineer.common.exception;

import com.lazybones.base.engineer.common.enums.StatusEnum;
import com.lazybones.base.engineer.common.res.ResultRes;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpClientErrorException;
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResultRes error(Exception e){
        e.fillInStackTrace();
        return ResultRes.error();
    }
    //指定异常方法
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public ResultRes error(NullPointerException e){
        e.fillInStackTrace();
        return ResultRes.setResult(StatusEnum.NULL_ERROR);
    }
    @ExceptionHandler(HttpClientErrorException.class)
    @ResponseBody
    public ResultRes error(HttpClientErrorException e){
        e.fillInStackTrace();
        return ResultRes.setResult(StatusEnum.HTTP_CLIENT_ERROR);
    }
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseBody
    public  ResultRes error(IllegalArgumentException e ){
        e.fillInStackTrace();
        return ResultRes.setResult(StatusEnum.PARAM_Null_MAYBE);
    }
    @ExceptionHandler(IndexOutOfBoundsException.class)
    @ResponseBody
    public ResultRes error(IndexOutOfBoundsException e){
        e.fillInStackTrace();
        return ResultRes.setResult(StatusEnum.INDEX_OUT_ERROR);
    }
    //自定义异常处理
    @ExceptionHandler(LazyBonesException.class)
    @ResponseBody
    public ResultRes error(LazyBonesException e){
        e.fillInStackTrace();
        return ResultRes.error().message(e.getMessage()).code(e.getCode());
    }
 /*   // 全局异常拦截
    @ExceptionHandler
    @ResponseBody
    public ResultRes handlerException(Exception e) {
        e.fillInStackTrace();
        return ResultRes.setResult(StatusEnum.HTTP_CLIENT_ERROR);
    }*/
}
